<?php

class Adept_Dispatcher 
{

    protected $rountes = array();
    protected $defaultController = null;

    public function dispatch($request, $response) 
    {
        foreach ($this->rountes as $route) {
            if ($route->check($request)) {
                $route->apply($request);
                if (null !== $route->getController()) {
                    $request->setController($route->getController());
                } else {
                    $request->setController($this->getDefaultController());
                }
                return true;
            }
        }
        return false;
    }

    public function addRoute($route) 
    {
        $this->rountes[] = $route;
        return $this;
    }

    public function addNewRoute($pattern, $controller, $params = array()) 
    {
        $this->addRoute(new Adept_Dispatcher_Route($pattern, $controller, $params));
        return $this;
    }

    public function getDefaultController() 
    {
        return $this->controller;
    }

    public function setDefaultController($controller) 
    {
        $this->controller = $controller;
    }

}